﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Summary description for clsEmail
/// </summary>
public class clsEmail
{
	public clsEmail()
	{
		//
		// TODO: Add constructor logic here
		//
	}


    /// <summary>
    /// Gửi Mail góp ý cho hội đồng quản trị
    /// </summary>
    /// <param name="strTieuDe">Tiêu đề góp ý</param>
    /// <param name="strEmail">Email gửi góp ý</param>
    /// <param name="strNoiDung">Nội dung góp ý</param>
    /// <returns></returns>
    public static bool Send_Mail_GopY(string strTieuDe, string strEmail, string strNoiDung)
    {
        //Gửi mail
        string strHtml = OsPortal.oFileHelper.ReadFile(basConst.LinkFormMailGopY);
        strHtml = strHtml.Replace("EMAIL_GOP_Y", strEmail);
        strHtml = strHtml.Replace("NOI_DUNG_GOP_Y", strNoiDung);

        return SendMailFromXomTroSV("nguyen7huan@gmail.com, nguyenduc.ak@gmail.com, nguyentv91@gmail.com", "[XomTroSV.com - Có Góp Ý Mới] - " + strTieuDe, strHtml);
    }

    /// <summary>
    /// Gửi Email kích hoạt tài khoản
    /// </summary>
    /// <param name="strHoTen">Họ tên người gửi</param>
    /// <param name="strEmail">Email</param>
    /// <param name="strMaKichHoat">Mã kích hoạt</param>
    /// <returns></returns>
    public static bool Send_Mail_Active_Account(string strHoTen, string strEmail, string strMaKichHoat)
    {
        //Gửi mail
        string strNoiDung = OsPortal.oFileHelper.ReadFile(basConst.LinkFormMailActiveAccount);
        strNoiDung = strNoiDung.Replace("HO_TEN_USER", strHoTen);
        strNoiDung = strNoiDung.Replace("EMAIL_DA_DANG_KY", strEmail);
        strNoiDung = strNoiDung.Replace("CODE_KICH_HOAT", strMaKichHoat);

        return SendMailFromXomTroSV(strEmail, "Kích hoạt tài khoản trên XomTroSV.com", strNoiDung);
    }


    /// <summary>
    /// Gửi email từ xomtrosv.com
    /// </summary>
    /// <param name="strToMail">Địa chỉ email nhận thư</param>
    /// <param name="strNoiDung"></param>
    /// <returns></returns>
    public static bool SendMailFromXomTroSV(string strToMail, string strTieuDe, string strNoiDung)
    {
        string strUserName = System.Configuration.ConfigurationManager.AppSettings["USERNAME_EMAIL"].ToString();
        string strPassWord = System.Configuration.ConfigurationManager.AppSettings["PASSWORD_EMAIL"].ToString();
        return sendGMail(strUserName, strPassWord, "XomTroSV.com", "xomtrosv.com@gmail.com", strToMail, strTieuDe, strNoiDung, "gmail");
    }

    /// <summary>
    /// Gửi Email Lấy lại mật khẩu
    /// </summary>
    /// <param name="strHoTen">Họ tên người gửi</param>
    /// <param name="strEmail">Email</param>
    /// <param name="strMaKichHoat">Mã kích hoạt</param>
    /// <returns></returns>
    public static bool Send_Mail_GetPassWord(string strEmail, string strMatKauMoi)
    {
        //Gửi mail
        string strNoiDung = OsPortal.oFileHelper.ReadFile(basConst.LinkFormGetPassword);
        strNoiDung = strNoiDung.Replace("EMAIL_USER", strEmail);
        strNoiDung = strNoiDung.Replace("MAT_KHAU_MOI_CAP_LAI", strMatKauMoi);

        return SendMailFromXomTroSV(strEmail, "Mật khẩu mới trên hệ thống XomTroSV.com", strNoiDung);
    }


    /// <summary>
    /// Gửi Email
    /// </summary>
    /// <param name="strUserName">UserName</param>
    /// <param name="strPassWord">Password</param>
    /// <param name="strName">Họ tên người gửi</param>
    /// <param name="strFrom">Địa chỉ mail của người gửi</param>
    /// <param name="strTo">Địa chỉ mail của người nhận</param>
    /// <param name="strSubject">Chủ Đề</param>
    /// <param name="strContent">Nội dung</param>
    /// <param name="strTypeMail">sử dụng "gmail" hay "yahoo"</param>
    /// <returns></returns>
    public static bool sendGMail(string strUserName,
                        string strPassWord,
                        string strName,
                        string strFrom,
                        string strTo,
                        string strSubject,
                        string strContent,
                        string strTypeMail)
    {
        MailMessage mailMsg;
        SmtpClient client;
        NetworkCredential info;
        switch (strTypeMail)
        {
            case "gmail":
                try
                {
                    mailMsg = new MailMessage();
                    mailMsg.From = new MailAddress(strFrom, strName);
                    mailMsg.To.Add(strTo);
                    mailMsg.Subject = strSubject;
                    mailMsg.IsBodyHtml = true;
                    mailMsg.Body = strContent;
                    mailMsg.Priority = MailPriority.Normal;
                    client = new SmtpClient("smtp.gmail.com", 587);
                    info = new NetworkCredential(strUserName, strPassWord);
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Credentials = info;
                    client.EnableSsl = true;
                    client.Send(mailMsg);
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                    HttpContext.Current.Response.Write(e);
                }

            case "yahoo":
                try
                {
                    mailMsg = new MailMessage();
                    mailMsg.From = new MailAddress(strFrom, strName);
                    mailMsg.To.Add(strTo);
                    mailMsg.Subject = strSubject;
                    mailMsg.IsBodyHtml = true;
                    mailMsg.Body = strContent;
                    mailMsg.Priority = MailPriority.Normal;
                    client = new SmtpClient("smtp.mail.yahoo.com", 587);
                    info = new NetworkCredential(strUserName, strPassWord);
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Credentials = info;
                    client.EnableSsl = true;
                    client.Send(mailMsg);
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                    HttpContext.Current.Response.Write(e);
                }
        }
        return false;
    }

}